<?php

class Branches extends Table
{
    // Ecrit le tableau des branches
    public static function Tableau($db)//-------------------------------------------------
    {
        $sql="SELECT bra_id, bra_code, bra_nom".
            ' FROM branches';
        $champs = array ('bra_code', 'bra_nom');
        $msg_vide = LNG_BRA_VIDE;
		return parent::Tableau($db, $sql, $champs, $champs, $msg_vide, True); 
    }//------------------------------------------------------------------Tableau

	public static function Mailing($db, $id){
		return parent::Mailing($db, $id, 'branches');	
	}

    public static function Select($db, $idexclu=-1)
    {
        $sql="SELECT bra_id, bra_code, bra_nom".
            ' FROM branches'.
            " WHERE bra_id!=$idexclu".
            ' ORDER BY bra_code ASC';
        if ($result = $db->query($sql))
		{
			if ( $result->num_rows<1 )
				return False;
			$retour = Array();
			while ( $row = $result->fetch_assoc() )
				$retour[] = Array (	$row['bra_id'],
									$row['bra_code'].' - '.$row['bra_nom']);
			return $retour;
		}
		return False;
    }

    public static function Code2Id($db,$code)
    {
		$sql="SELECT bra_id FROM branches WHERE bra_code='$code'";
        if ($result = $db->query($sql))
		{
			if ( $result->num_rows<1 )
				return False;
			$row = $result->fetch_assoc();
			return $row['bra_id'];
		}
		return False;
	}
}

class Branche extends Enregistrement
{
	// Constructeur
	function __construct($db)//-------------------------------------------------
	{
		$this->defaut = Array('bra_id' 	=> -1,
						'bra_code' 		=> '',					
						'bra_nom' 		=> '');						
		parent::__construct($db, 'branches', 'bra_id');
	}//--------------------------------------------------------------__construct

    
    function Supprimer($remplacant){
		Clients::UpdateBra($this->db, $this->attribut['bra_id'], $remplacant); 
echo 'test';
		$sql = "DELETE FROM branches WHERE bra_id={$this->id}";
		return ( $result = $this->db->query($sql) );
    }

	function Traitement(&$erreur){
		if ( isset( $_POST['maj_infos'] ) )
		{
			$this->LirePost();
			$this->Verification();
			if ( $this->VerifOk() )
				if ( $this->Ecrire() )
				{
					$_SESSION[SES_STATUT] = LNG_STA_MAJ_0;
					$id = $this->cle();
				}
				else
					$_SESSION[SES_STATUT] = LNG_STA_ERREUR.LNG_STA_MAJ_1;
		}
		elseif ( isset( $_POST['maj_sup'] ) )
		{
			if ( isset( $_POST['confirmation'] ) ){
				if ($_POST['remplacant']==-1) {
					$erreur['remplacant'] = LNG_UTI_REMPLACANT_AIDE;
				}
				else {
					if ( $this->Supprimer($_POST['remplacant']) ){
						$_SESSION[SES_STATUT] = LNG_STA_UTI_SUP_0;
						header('location:branches.php');
					}
					else{
						$_SESSION[SES_STATUT] = LNG_STA_ERREUR.LNG_STA_UTI_SUP_1;
					}
				}
			}
			else
				$erreur['confirmation'] = LNG_UTI_CONFIRMATION_AIDE;
		}		
	}//-------------------------------------------------------------------------

	// Verification des attributs via la base
	function Verification()//---------------------------------------------------
	{
		// Verif de l'unicité du code
		$this->erreurs['bra_code'] = '';
		if ( trim($this->attribut['bra_code']) == '' )
			$this->erreurs['bra_code'] = LNG_CHAMP_OBLIGATOIRE;
		if ( trim($this->attribut['bra_nom']) == '' )
			$this->erreurs['bra_nom'] = LNG_CHAMP_OBLIGATOIRE;
		else
		{
			if ( !$this->db->Unique('branches', 'bra_code', $this->attribut['bra_code'], 'bra_id', $this->attribut['bra_id'] ) )
				$this->erreurs['bra_code'] .= LNG_UTI_CODEEXISTANT;
		}		
		return $this->erreurs;
	}//-------------------------------------------------------------Verification

	function NomFormat()
	{
		return parent::NomFormat('bra_nom');
	}
	
	function Contenu($erreur,$onglet=0, $edition=False)//-----------------------
	{
		switch ($onglet)
		{
			case 0:
				$h4 = LNG_H4_INFORMATIONS;
				if ( droit_modifier(GR_BRA_MODIF, $edition) )
					$h4 .= Liens::BraModifier($this->cle());
?>
<form method='post' action='branche.php?<?="id={$this->cle()}&edition=$edition"?>'>
<?php
				dohtml_h4($h4);
				$this->ChampText('bra_code', $edition);
				$this->ChampText('bra_nom', $edition);
				if ($edition)
					dohtml_formbouton('labelagauche', 'Mettre à jour', 'maj_infos', 'Annuler');
?>
</form>



				<?php
				if ($this->cle() != -1 && $_SESSION[SES_USER_NIVEAU]>=GR_BRA_SUPPR)
				{
				?>

				<form method='post' action='branche.php?<?="id={$this->cle()}&amp;edition=$edition"?>'>

				<?php	
						dohtml_h4(LNG_H4_SUPPRESSION);
						dohtml_formcheck('labelagauche',
										'confirmation',
										LNG_UTI_CONFIRMATION,
										LNG_UTI_CONFIRMATION_AIDE,
										$erreur['confirmation']);
						dohtml_formselect('labelagauche',
										'remplacant',
										LNG_UTI_REMPLACANT,
										LNG_UTI_REMPLACANT_AIDE,
										branches::select($this->db, $this->attribut['bra_id']),
										True,
										-1,
										$erreur['remplacant']);
						dohtml_formbouton('labelagauche', 'Supprimer', 'maj_sup');
				?>
				</form>

				<?php
				}			

				break;
			case 1:
				Clients::Tableau_bra($this->db, $this->cle());
				break;
		}
	}
}

?>
